﻿using SelfMadeMillionaire.Core.Entities;
using SelfMadeMillionaire.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SelfMadeMillionaire.DAL.Repositories
{
    public class SymbolOptionsHistoryRepository : Repository<SymbolOptionsHistory>, ISymbolOptionsHistoryRepository
	{
        public SymbolOptionsHistoryRepository(IUnitOfWork uow)
			: base(uow)
		{
		}

        public IEnumerable<SymbolOptionsHistory> Search(bool isCalls, int daysInTrade, double minDiscount, string minAnnualReturnField, double minAnnualReturn, double minProbability, string symbols, string sortOrder)
        {
            DataContext context = new DataContext();

            List<SqlParameter> sqlParams = new List<SqlParameter>();
            sqlParams.Add(new SqlParameter("@IsCalls", isCalls));
            sqlParams.Add(new SqlParameter("@DaysInTrade", daysInTrade));
            sqlParams.Add(new SqlParameter("@Discount", minDiscount));
            sqlParams.Add(new SqlParameter("@AnnualReturnField", minAnnualReturnField));
            sqlParams.Add(new SqlParameter("@AnnualReturn", minAnnualReturn));
            sqlParams.Add(new SqlParameter("@Probability", minProbability));
            sqlParams.Add(new SqlParameter("@Symbols", symbols));

            IEnumerable<SymbolOptionsHistory> list = context.Database.SqlQuery<SymbolOptionsHistory>(@"spSearchSymbolOptionsHistory @IsCalls = @IsCalls, 
                    @DaysInTrade = @DaysInTrade, @Discount = @Discount, 
                    @AnnualReturnField = @AnnualReturnField, @AnnualReturn = @AnnualReturn,
                    @Probability = @Probability, @Symbols = @Symbols", sqlParams.ToArray()).ToList();

            if (!string.IsNullOrEmpty(sortOrder))
            {
                string[] sortArr = sortOrder.Split(',');
                if (sortArr[1] == "0")
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderBy(i => i.ContractName);
                            break;
                        case "1":
                            list = list.OrderBy(i => i.Symbol);
                            break;
                        case "2":
                            list = list.OrderBy(i => i.OptionDate);
                            break;
                        case "3":
                            list = list.OrderBy(i => i.SymbolPrice);
                            break;
                        case "4":
                            list = list.OrderBy(i => i.Strike);
                            break;
                        case "5":
                            list = list.OrderBy(i => i.Discount);
                            break;
                        case "6":
                            list = list.OrderBy(i => i.Bid);
                            break;
                        case "7":
                            list = list.OrderBy(i => i.Ask);
                            break;
                        case "8":
                            list = list.OrderBy(i => i.Last);
                            break;
                        case "9":
                            list = list.OrderBy(i => i.Volume);
                            break;
                        case "10":
                            list = list.OrderBy(i => i.OpenInterest);
                            break;
                        case "11":
                            list = list.OrderBy(i => i.ImpliedVolatility);
                            break;
                        case "12":
                            list = list.OrderBy(i => i.Probability);
                            break;
                        case "13":
                            list = list.OrderBy(i => i.BidAnnualReturn);
                            break;
                        case "14":
                            list = list.OrderBy(i => i.AskAnnualReturn);
                            break;
                        case "15":
                            list = list.OrderBy(i => i.BidAsk2);
                            break;
                        case "16":
                            list = list.OrderBy(i => i.LastAnnualReturn);
                            break;
                        case "17":
                            list = list.OrderBy(i => i.DaysInTrade);
                            break;

                    }
                }
                else
                {
                    switch (sortArr[0])
                    {
                        case "0":
                            list = list.OrderByDescending(i => i.ContractName);
                            break;
                        case "1":
                            list = list.OrderByDescending(i => i.Symbol);
                            break;
                        case "2":
                            list = list.OrderByDescending(i => i.OptionDate);
                            break;
                        case "3":
                            list = list.OrderByDescending(i => i.SymbolPrice);
                            break;
                        case "4":
                            list = list.OrderByDescending(i => i.Strike);
                            break;
                        case "5":
                            list = list.OrderByDescending(i => i.Discount);
                            break;
                        case "6":
                            list = list.OrderByDescending(i => i.Bid);
                            break;
                        case "7":
                            list = list.OrderByDescending(i => i.Ask);
                            break;
                        case "8":
                            list = list.OrderByDescending(i => i.Last);
                            break;
                        case "9":
                            list = list.OrderByDescending(i => i.Volume);
                            break;
                        case "10":
                            list = list.OrderByDescending(i => i.OpenInterest);
                            break;
                        case "11":
                            list = list.OrderByDescending(i => i.ImpliedVolatility);
                            break;
                        case "12":
                            list = list.OrderByDescending(i => i.Probability);
                            break;
                        case "13":
                            list = list.OrderByDescending(i => i.BidAnnualReturn);
                            break;
                        case "14":
                            list = list.OrderByDescending(i => i.AskAnnualReturn);
                            break;
                        case "15":
                            list = list.OrderByDescending(i => i.BidAsk2);
                            break;
                        case "16":
                            list = list.OrderByDescending(i => i.LastAnnualReturn);
                            break;
                        case "17":
                            list = list.OrderByDescending(i => i.DaysInTrade);
                            break;

                    }
                }
            }

            return list;
        }
	
	}
}
